const c = window.cloud
Vue.prototype.cloud = c
Vue.config.productionTip = false //阻止vue在启动时生成生产提示
Vue.config.devtools = true

var a = new Vue({
    el: "#findPassword",
    data() {
        return {
            Email: "",
            code: "",
            inputCode: "",
            newPasswordFirst: "",
            newPasswordSecond: "",
            canGetCode: true,
            waitTime: 61,
        }
    },
    methods: {
        async initWx() {
            //创建新的cloud实例
            var c1 = new this.cloud.Cloud({
                identityless: true,
                // 资源方 AppID 填自己的
                resourceAppid: 'wx96f760966921f98a',
                // 资源方环境 ID	填自己的
                resourceEnv: 'cloud1-8g54d6xb74e96e7d'
            })
            //初始化,使用await将异步转化为同步
            await c1.init()
            console.log('初始化完成');
            this.cloud = c1
            this.db = this.cloud.database()
        }
        ,
        //返回上一页面
        goBack() {
            // alert("返回");
            window.history.back(-1);
        },

        inputEmail() {
            let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
            let email = this.Email
            if (str.test(email)) {

                return true

            } else {
                return false
            }
        },
        //获取验证码
        getCode() {
            // 0.先判断邮箱格式是否正确
            //                         ---->正确则判断邮箱是否存在
            let inputLeagl = this.inputEmail();
            if (inputLeagl) {
                this.db.collection("operationUsers").where({ email: this.Email }).get().then(res => {
                    if (res.data.length) {
                        //1.
                        //先设置为不能连续点击
                        this.canGetCode = false                           //先设置为不能连续点击
                        const that = this;                              //获取验证码间间隔
                        that.waitTime--;                                //一调用先时间减少1
                        console.log('获取时间的值：' + this.waitTime);
                        const timer = setInterval(function () {
                            if (that.waitTime > 1) {                        //倒计时未结束
                                that.waitTime--
                                // console.log(that.waitTime)           //每一秒的倒数都输出一下
                            }
                            else {
                                clearInterval(timer)
                                that.canGetCode = true                    //可以再次发送验证码
                                that.waitTime = 61
                            }
                        }, 1000)
                        //2.
                        //生成随机验证码
                        console.log(1)
                        var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";   //验证码中可能包含的字符
                        var b = "";                                                                 //保存生成的验证码，验证码是一个字符串类型
                        for (var i = 0; i < 4; i++) {
                            var index = Math.floor(Math.random() * 62)                              //随机产生0-62个之间随机数
                            b = b + s.charAt(index);                                                //将随机产生的数字当做字符串的下标，然后用charAt函数将它取出来然后放入b中
                        }
                        // this.code = b                                                            //将不再通过data保存生成的验证码的值
                        var em = this.Email
                        // 3.
                        //通过cookie设置临时变量保存验证码
                        var name = 'yanzhengma'
                        var zhi = b                                            //生成的验证码为cookie的值
                        this.$cookies.set(
                            name, zhi,                                           //设置cookie名和值
                            "1800s"                                         //设置cookie的有效时间，经过此时间后自动消失
                        );                                                      //30分钟内有效
                        // console.log(this.$cookies.get(name))
                        //4.
                        //发送验证码
                        //初始化

                        //先判断邮箱是否是允许的
                        // 发送邮件验证码
                        this.cloud.callFunction({
                            name: 'sendEmail',              //调用发送邮件的云方法
                            data: {
                                "verificationCode": b,      //随机验证码
                                "toEmail": em,           //输入的邮箱号
                                'subject':'找回密码',
                                'context':"尊敬的用户您好，找回密码的验证码为"+b
                            },
                        }).then(res => {
                            console.log("发送结果");
                            console.log(res);
                        });








                     
                    } else {
                        this.$message({
                            message: '不存在该用户!',
                            type: 'warning'
                        });
                    }
                })
            } else {
                
                this.$message({
                    message: '邮箱格式不合法!',
                    type: 'warning'
                });
            }
        },

        //提交重置信息
        submit() {
            if (this.newPasswordFirst == 0) {
                this.$message({
                    message: '新密码不能为空！!',
                    type: 'warning'
                });
            }
            else {
                if (this.newPasswordFirst.length < 8) {
                    this.$message({
                        message: '密码过短!',
                        type: 'warning'
                    });
                }
                else {
                    if (this.inputCode.length == 0) {
                        this.$message({
                            message: '验证码不能为空！',
                            type: 'warning'
                        });
                    }
                    else {
                        if (this.$cookies.get('yanzhengma') == this.inputCode) {
                            if (this.newPasswordFirst == this.newPasswordSecond) {
                                ////////////////////////////////////////////////////////
                                //修改密码
                                var em = this.email
                                var p = this.newPasswordFirst
                                
                               
                                    this.db.collection('operationUsers').where({ email: em }).update({
                                        data: {
                                            webPassword: p
                                        },
                                        success: res => {
                                            console.log("修改成功！");
                                            this.$message({
                                                message: '修改成功',
                                                type: 'success'
                                            });
                                        }
                                    })

                                
                                this.$cookies.remove('yanzhengma')//一旦成功修改密码立刻删除该cookie
                                location.href = './index.html'
                            }
                            else {
                                this.$message({
                                    message: '两次密码不一致！',
                                    type: 'warn'
                                });
                            }
                        }
                        else {
                           
                            this.$message({
                                message: '验证码错误！',
                                type: 'error'
                            });
                            
                        }
                    }
                }

            }
        }


    },
    async created() {
        await this.initWx()
    },
})
